package com.sduept.zj103;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.ssssssss.magicapi.Const;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.core.TioConfig;
import org.tio.core.exception.TioDecodeException;
import org.tio.core.intf.Packet;
import org.tio.server.intf.TioServerHandler;

import com.sduept.tool.CrcUtil;
import com.sduept.tool.PrintTool;
import com.sduept.zj103.model.Zj103Protocol;

/**
 * @author zcg
 */
//@Component
public class Zj103ServerHandler implements TioServerHandler {
	private static Logger logger = LoggerFactory.getLogger(Zj103ServerHandler.class);
   /**
     * 解码：把接收到的ByteBuffer，解码成应用可以识别的业务消息包
     * 总的消息结构：消息头 + 消息体
     * 消息头结构：    4个字节，存储消息体的长度
     * 消息体结构：   对象的json串的byte[]
     */
    @Override
    public Zj103Protocol decode(ByteBuffer buffer, int limit, int position, int readableLength, ChannelContext channelContext) throws TioDecodeException {
        if(readableLength < 4) {
            return null;
        }

        buffer.order(ByteOrder.LITTLE_ENDIAN);
        int msgLen = buffer.getShort();
        Zj103Protocol zj103packet = new Zj103Protocol();

        return zj103packet;
    }
    /**
     * 编码：把业务消息包编码为可以发送的ByteBuffer
     * 总的消息结构：消息头 + 消息体
     * 消息头结构：    4个字节，存储消息体的长度
     * 消息体结构：   对象的json串的byte[]
     */
    @Override
    public ByteBuffer encode(Packet packet, TioConfig tioConfig, ChannelContext channelContext) {
        try {
            ByteBuffer byteBuffer = ByteBuffer.allocate(27);

            return byteBuffer;
        } catch (Exception e) {
            logger.error("encode异常", e);
            return null;
        }
    }
    /**
     * 处理消息
     */
    @Override
    public void handler(Packet packet, ChannelContext channelContext) throws Exception {
    }

}